import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

//课程的页面
import Course from '@/views/course/Course.vue'
import CourseInfo from '@/views/course/CourseInfo.vue'
import CoursePlay from '@/views/course/CoursePlay.vue'

//会员
import Member from '@/views/Member.vue'

//购物车
import Cart from '@/views/cart/Cart.vue'
import ConfirmOrder from '@/views/cart/ConfirmOrder.vue'

//我的课程
import About from '@/views/about/About.vue'
import MyCourse from '@/views/about/components/MyCourse.vue'
import Collean from '@/views/about/components/Collean.vue'

//个人中心
import User from '@/views/user/User.vue'
import Setbindsns from '@/views/user/components/Setbindsns.vue'
import Setavatar from '@/views/user/components/Setavatar.vue'

Vue.use(VueRouter)

const routes = [
  { path: '/',name: 'home',component: Home },
  { path: '/course',name: 'course',component: Course },
  { path: '/course-info/:courseId',name: 'courseinfo',component: CourseInfo },
  { path: '/course-play/:courseId/:chapterId',name: 'courseplay',component: CoursePlay },

  { path: '/member',name: 'member',component: Member },

  { path: '/cart',name: 'cart',meta:{requiresAuth:true},component: Cart },
  { path: '/confirmOrder',name: 'confirmOrder',meta:{requiresAuth:true},component: ConfirmOrder },

  //我的课程的路由
  { path: '/about',name: 'about',component: About, reditect:'/about/my-course',meta:{requiresAuth:true},
    children:[
      {
        path:'my-course',
        name:'my-course',
        meta:{requiresAuth:true},
        component: MyCourse
      },
      {
        path:'collean',
        name:'collean',
        meta:{requiresAuth:true},
        component: Collean

      }
    ]
  },

  //个人中心的路由
  { path: '/user',name: 'user',component: User, reditect:'/user/setbindsns',meta:{requiresAuth:true},
    children:[
      {
        path:'setbindsns',
        name:'setbindsns',
        meta:{requiresAuth:true},
        component: Setbindsns
      },
      {
        path:'setavator',
        name:'setavator',
        meta:{requiresAuth:true},
        component: Setavatar
      }
    ]
  },
  // {
  //   path: '/about',
  //   name: 'about',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  // }
]

const router = new VueRouter({
  routes
})

//导航守卫
router.beforeEach((to,from,next) => {
  let isLogin = localStorage.getItem('isLogin')
  if(to.matched.some(rec => rec.meta.requiresAuth)){
    if(!isLogin){
      //没有登录
      console.log('未登录')
    }else{
      next()
    }
  }else{
    next()
  }
})

export default router
